package com.zsp.result;


import com.alibaba.fastjson.annotation.JSONField;
import java.io.Serializable;

public class Result<T> implements Serializable {
    @JSONField(
            name = "State"
    )
    private int state;
    @JSONField(
            name = "Msg"
    )
    private String msg;
    @JSONField(
            name = "Body"
    )
    private T body;
    @JSONField(
            name = "Total"
    )
    private Long total;
    @JSONField(
            name = "Code"
    )
    private int code;
    @JSONField(
            name = "Success"
    )
    private Boolean success;


    public Result() {
        this(ResultStateEnum.FAIL);
    }

    public Result(int state, String message) {
        this.state = state;
        this.msg = message;
        this.total = 0L;
    }

    public Result(int state, String message, T body) {

        this.state = state;
        this.msg = message;
        this.body = body;
        this.total = 0L;
    }

    public Result(AppState appState) {
        this.state = appState.getState();
        this.msg = appState.getMessage();
        this.total = 0L;
        this.code = this.state;
    }

    public Result(AppState appState, T body) {

        this.state = appState.getState();
        this.msg = appState.getMessage();
        this.body = body;
        this.total = 0L;
    }

    public Result(int state, String msg, T body, Long total) {

        this.state = state;
        this.msg = msg;
        this.body = body;
        this.total = total;
    }

    public static Result<String> fail(String msg) {
        return fail(ResultStateEnum.FAIL.getState(), msg);
    }

    public static Result fail(String msg, Object body) {
        return new Result(-1, msg, body);
    }

    public static Result<String> error(String msg) {
        return fail(ResultStateEnum.ERROR.getState(), msg);
    }

    public static Result<String> fail(int state, String msg) {
        return new Result(state, msg);
    }

    public static Result<String> success(String msg) {
        return success(msg, (Object) null);
    }

    public static Result success(String msg, Object body) {
        return new Result(ResultStateEnum.SUCCESS.getState(), msg, body);
    }

    public void ok() {
        this.state = ResultStateEnum.SUCCESS.getState();
        this.msg = ResultStateEnum.SUCCESS.getMessage();
    }

    public int getState() {
        return this.state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getBody() {
        return this.body;
    }

    public void setBody(T body) {
        this.body = body;
    }

    public Long getTotal() {
        return this.total;
    }

    public int getCode() {
        return this.state;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
    public static <T> Result<T> success(T data) {
        return buildResult(ResultStateEnum.SUCCESS.getState(), ResultStateEnum.SUCCESS.getMessage(), data);
    }
    private static <T> Result<T> buildResult(int state, String message, T data) {
        return new Result<>(state, message, data);
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Boolean getSuccess() {
        return this.state == 1 ? true : false;
    }

    public String toString() {
        return "Result{state=" + this.state + ", msg='" + this.msg + '\'' + ", body=" + this.body + ", total=" + this.total + '}';
    }
}
